/*------------------------------------------------------------------------
|                            FILE DESCRIPTION                            |
------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
|  - File name     : os_task.h
|  - Author        : zeweni
|  - Update date   : 2021.03.25
|  - Copyright(C)  : 2021-2021 zeweni. All rights reserved.
------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
|                            COPYRIGHT NOTICE                            |
------------------------------------------------------------------------*/
/*
 * Copyright (C) 2021, zeweni (17870070675@163.com)

 * This file is part of Ant Real Time Operating System.

 * Ant Real Time Operating System is free software: you can redistribute 
 * it and/or modify it under the terms of the Apache-2.0 License.

 * Ant Real Time Operating System is distributed in the hope that it will 
 * be useful,but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * Apache-2.0 License License for more details.

 * You should have received a copy of the Apache-2.0 License.Ant Real Time
 * Operating System. If not, see <http://www.apache.org/licenses/>.
**/
/*------------------------------------------------------------------------
|                                INCLUDES                                |
------------------------------------------------------------------------*/
#ifndef __OS_TASK_H_
#define __OS_TASK_H_

#include "os_config.h"
#include "type_os.h"
#include "list_os.h"

/*------------------------------------------------------------------------
|                                  DATA                                  |
------------------------------------------------------------------------*/

typedef enum
{
	OSTask_Runing = 0x00,
	OSTask_Ready  = 0x01,
	OSTask_Sleep  = 0x02,
	OSTask_Block  = 0x03
} TaskState_t;

/**
  * @name    OS_TCB_t
  * @brief   Task control blocks struct.
  * @param   list [OS_List_t]
  * @param   TaskID [uint8_t]
  * @param   TaskPrio [uint8_t]
  * @param   TaskState [TaskState_t]
  * @param   TaskDelay [uint16_t]
  * @param   StkAddr [uint8_t *]
  * @param   StkSize [uint8_t]
***/
typedef struct TCB
{
	os_uint8_t TaskID;
	os_uint8_t TaskPrio;
	TaskState_t TaskState;
	os_uint16_t TaskSlp;
	os_uint8_t *StkAddr;
	os_uint8_t StkSize;
} OS_TCB_t;


/**
 * @brief   就绪任务链表结构体重定义。
 * @details The weight definition of the structure of the ready task list.
**/
typedef struct
{
	OS_TCB_t xdata *RdyPtr[OS_TASK_MAX_NUM];
	os_uint8_t RdyNum;
}TCB_RdyList_t;


/**
 * @brief   阻塞任务链表结构体重定义。
 * @details The weight definition of the structure of the blocking task list.
**/
typedef struct
{
	OS_TCB_t xdata *BlkPtr[OS_TASK_MAX_NUM];
	os_uint8_t BlkNum;
}TCB_BlkList_t;


/**
 * @brief   休眠任务链表结构体重定义。
 * @details The weight definition of the dormant task linked list structure.
**/
typedef struct
{
	OS_TCB_t xdata *SlpPtr[OS_TASK_MAX_NUM];
	os_uint8_t SlpNum;
}TCB_SlpList_t;


/**
  * @name    OS_Idle_Task_t
  * @brief   Idle Task function redefine type.
***/
typedef void (* OS_Idle_Task_t) (void);

/**
  * @name    OS_Idle_Task_Function
  * @brief   RTOS idle task handle function.
***/
extern OS_Idle_Task_t OS_Idle_Task_Function;


#define TASK_SELF   0xEF

/*------------------------------------------------------------------------
|                              API FUNCTIONS                             |
------------------------------------------------------------------------*/

/**
  * @name    OS_Task_Create
  * @brief   Create task threads,where in the rtos kernel static memory bolcks.
  * @brief   Because using dynamic memory blocks requires more code and time.
  * @param   pTaskFun [IN] Task function address.
  * @param   tskID [IN] Task id.
  * @param   tskPrio [IN] Task priority,
  *          creating tasks of the same priority is not supported. 
  * @param   *pStk [IN] Task stack address.
  * @param   stkSize [IN] Task stack size.
  * @retval  [OS_TCB_t *] Create Task success,task tcb handle returned.
  * @retval  [NULL] Task created failed.
***/
extern os_uint8_t OS_Task_Create(void(*entry)(void), 
                                  os_uint8_t tskPrio,
                                  os_uint8_t *pStk,
                                  os_uint8_t stkSize);

/**
  * @name    OS_Task_Delete
  * @brief   Delete task threads.
  * @param   tskID [IN] Task id.
  * @retval  [OS_OK] Task deleted successfully.
  * @retval  [OS_NOK] Task deleted failed.
***/
extern  os_uint8_t OS_Task_Delete(os_uint8_t tskID);

/**
  * @name    OS_Task_Sleep
  * @brief   Delete task threads.
  * @param   ticks [IN] Task sleep ticks.
  * @retval  None.
***/
extern void OS_Task_Sleep(os_uint16_t ticks);

#endif
/*------------------------------------------------------------------------
|                    END OF FLIE.  (C) COPYRIGHT zeweni                  |
------------------------------------------------------------------------*/